{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basics of Numpy and Pandas\n",
    "### Dr. Tirthajyoti Sarkar, Fremont, CA 94536\n",
    "\n",
    "---\n",
    "\n",
    "This notebook discusses basics of two most important Python libraries for data analytics and statistical modeling - `Numpy` and `Pandas`,\n",
    "\n",
    "### Numpy\n",
    "\n",
    "---\n",
    "\n",
    "* Numpy array - from list, special functions\n",
    "* Array operations\n",
    "* 2-D arrays\n",
    "* Indexing and slicing\n",
    "* Conditional subsetting\n",
    "* Array-array operations\n",
    "\n",
    "### Pandas\n",
    "\n",
    "---\n",
    "\n",
    "* Pandas series\n",
    "* DataFrame - creation, read from files\n",
    "* Quick checking DataFrame\n",
    "* Descriptive stats on DataFrame\n",
    "* Indexing, slicing, conditional subsetting\n",
    "* Operations on specific rows/columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numpy array from a Python list\n",
    "Numpy arrays behave like **true numerical vectors**, not ordinary lists. That's why they are used for all mathematical operations, machine learning algorithms, and as basis of Pandas DataFrame for data analytics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "lst1=[1,2,3]\n",
    "array1 = np.array(lst1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(lst1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(array1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst2=[10,11,12]\n",
    "array2 = np.array(lst2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding two lists [1, 2, 3] and [10, 11, 12] together: [1, 2, 3, 10, 11, 12]\n"
     ]
    }
   ],
   "source": [
    "print(f\"Adding two lists {lst1} and {lst2} together: {lst1+lst2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding two numpy arrays [1 2 3] and [10 11 12] together: [11 13 15]\n"
     ]
    }
   ],
   "source": [
    "print(f\"Adding two numpy arrays {array1} and {array2} together: {array1+array2}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mathematical operations with/on Numpy arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array2 multiplied by array1:  [10 22 36]\n",
      "array2 divided by array1:  [10.   5.5  4. ]\n",
      "array2 raised to the power of array1:  [  10  121 1728]\n"
     ]
    }
   ],
   "source": [
    "print(\"array2 multiplied by array1: \",array1*array2)\n",
    "print(\"array2 divided by array1: \",array2/array1)\n",
    "print(\"array2 raised to the power of array1: \",array2**array1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sine:  [0.84147098 0.90929743 0.14112001]\n",
      "Natural logarithm:  [0.         0.69314718 1.09861229]\n",
      "Base-10 logarithm:  [0.         0.30103    0.47712125]\n",
      "Base-2 logarithm:  [0.        1.        1.5849625]\n",
      "Exponential:  [ 2.71828183  7.3890561  20.08553692]\n"
     ]
    }
   ],
   "source": [
    "# sine function\n",
    "print(\"Sine: \",np.sin(array1))\n",
    "# logarithm\n",
    "print(\"Natural logarithm: \",np.log(array1))\n",
    "print(\"Base-10 logarithm: \",np.log10(array1))\n",
    "print(\"Base-2 logarithm: \",np.log2(array1))\n",
    "# Exponential\n",
    "print(\"Exponential: \",np.exp(array1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to generate arrays easily?\n",
    "* `np.zeros`\n",
    "* `np.ones`\n",
    "* `np.arange`\n",
    "* `np.linspace`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A series of zeroes: [0. 0. 0. 0. 0. 0. 0.]\n",
      "A series of ones: [1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      "A series of numbers: [ 5  6  7  8  9 10 11 12 13 14 15]\n",
      "Numbers spaced apart by 2: [ 0  2  4  6  8 10]\n",
      "Numbers spaced apart by float: [ 0.   2.5  5.   7.5 10. ]\n",
      "Every 5th number from 30 in reverse order:  [30 25 20 15 10  5  0]\n",
      "11 linearly spaced numbers between 1 and 5:  [1.  1.4 1.8 2.2 2.6 3.  3.4 3.8 4.2 4.6 5. ]\n"
     ]
    }
   ],
   "source": [
    "print(\"A series of zeroes:\",np.zeros(7))\n",
    "print(\"A series of ones:\",np.ones(9))\n",
    "print(\"A series of numbers:\",np.arange(5,16))\n",
    "print(\"Numbers spaced apart by 2:\",np.arange(0,11,2))\n",
    "print(\"Numbers spaced apart by float:\",np.arange(0,11,2.5))\n",
    "print(\"Every 5th number from 30 in reverse order: \",np.arange(30,-1,-5))\n",
    "print(\"11 linearly spaced numbers between 1 and 5: \",np.linspace(1,5,11))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multi-dimensional arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type/Class of this object: <class 'numpy.ndarray'>\n",
      "Here is the matrix\n",
      "----------\n",
      " [[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]] \n",
      "----------\n"
     ]
    }
   ],
   "source": [
    "my_mat = [[1,2,3],[4,5,6],[7,8,9]]\n",
    "mat = np.array(my_mat)\n",
    "print(\"Type/Class of this object:\",type(mat))\n",
    "print(\"Here is the matrix\\n----------\\n\",mat,\"\\n----------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.5 2.  3. ]\n",
      " [4.  5.  6. ]]\n"
     ]
    }
   ],
   "source": [
    "my_tuple = np.array([(1.5,2,3), (4,5,6)])\n",
    "mat_tuple = np.array(my_tuple)\n",
    "print (mat_tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dimension, shape, size, and data type of the 2D array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dimension of this matrix: 2\n",
      "Size of this matrix: 9\n",
      "Shape of this matrix: (3, 3)\n",
      "Data type of this matrix: int32\n"
     ]
    }
   ],
   "source": [
    "print(\"Dimension of this matrix: \",mat.ndim,sep='') \n",
    "print(\"Size of this matrix: \", mat.size,sep='') \n",
    "print(\"Shape of this matrix: \", mat.shape,sep='')\n",
    "print(\"Data type of this matrix: \", mat.dtype,sep='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Zeros, Ones, Random, and Identity Matrices and Vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vector of zeros:  [0. 0. 0. 0. 0.]\n",
      "Matrix of zeros:  [[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "Vector of ones:  [1. 1. 1. 1.]\n",
      "Matrix of ones:  [[1. 1.]\n",
      " [1. 1.]\n",
      " [1. 1.]\n",
      " [1. 1.]]\n",
      "Matrix of 5’s:  [[5. 5. 5.]\n",
      " [5. 5. 5.]\n",
      " [5. 5. 5.]]\n",
      "Identity matrix of dimension 2: [[1. 0.]\n",
      " [0. 1.]]\n",
      "Identity matrix of dimension 4: [[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]\n",
      " [0. 0. 0. 1.]]\n",
      "Random matrix of shape (4,3):\n",
      " [[2 1 5]\n",
      " [3 3 1]\n",
      " [4 7 3]\n",
      " [2 8 8]]\n"
     ]
    }
   ],
   "source": [
    "print(\"Vector of zeros: \",np.zeros(5))\n",
    "print(\"Matrix of zeros: \",np.zeros((3,4)))\n",
    "print(\"Vector of ones: \",np.ones(4))\n",
    "print(\"Matrix of ones: \",np.ones((4,2)))\n",
    "print(\"Matrix of 5’s: \",5*np.ones((3,3)))\n",
    "print(\"Identity matrix of dimension 2:\",np.eye(2))\n",
    "print(\"Identity matrix of dimension 4:\",np.eye(4))\n",
    "print(\"Random matrix of shape (4,3):\\n\",np.random.randint(low=1,high=10,size=(4,3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reshaping, Ravel, Min, Max, Sorting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape of a: (30,)\n",
      "Shape of b: (2, 3, 5)\n",
      "Shape of c: (6, 5)\n"
     ]
    }
   ],
   "source": [
    "a = np.random.randint(1,100,30)\n",
    "b = a.reshape(2,3,5)\n",
    "c = a.reshape(6,5)\n",
    "print (\"Shape of a:\", a.shape)\n",
    "print (\"Shape of b:\", b.shape)\n",
    "print (\"Shape of c:\", c.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "a looks like:\n",
      " [71 47 34 56 80 14 46 51 92 33 87  7 37 73 74 66 81 84 94 81  2 95 88 32\n",
      " 68 14 60 97 85  5]\n",
      "\n",
      "b looks like:\n",
      " [[[71 47 34 56 80]\n",
      "  [14 46 51 92 33]\n",
      "  [87  7 37 73 74]]\n",
      "\n",
      " [[66 81 84 94 81]\n",
      "  [ 2 95 88 32 68]\n",
      "  [14 60 97 85  5]]]\n",
      "\n",
      "c looks like:\n",
      " [[71 47 34 56 80]\n",
      " [14 46 51 92 33]\n",
      " [87  7 37 73 74]\n",
      " [66 81 84 94 81]\n",
      " [ 2 95 88 32 68]\n",
      " [14 60 97 85  5]]\n"
     ]
    }
   ],
   "source": [
    "print(\"\\na looks like:\\n\",a)\n",
    "print(\"\\nb looks like:\\n\",b)\n",
    "print(\"\\nc looks like:\\n\",c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[71 47 34 56 80 14 46 51 92 33 87  7 37 73 74 66 81 84 94 81  2 95 88 32\n",
      " 68 14 60 97 85  5]\n"
     ]
    }
   ],
   "source": [
    "b_flat = b.ravel()\n",
    "print(b_flat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Indexing and slicing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array: [ 0  1  2  3  4  5  6  7  8  9 10]\n",
      "Element at 7th index is: 7\n",
      "Elements from 3rd to 5th index are: [3 4 5]\n",
      "Elements up to 4th index are: [0 1 2 3]\n",
      "Elements from last backwards are: [10  9  8  7  6  5  4  3  2  1  0]\n",
      "3 Elements from last backwards are: [10  8  6]\n",
      "New array: [ 0  2  4  6  8 10 12 14 16 18 20]\n",
      "Elements at 2nd, 4th, and 9th index are: [ 4  8 18]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(0,11)\n",
    "print(\"Array:\",arr)\n",
    "print(\"Element at 7th index is:\", arr[7])\n",
    "print(\"Elements from 3rd to 5th index are:\", arr[3:6])\n",
    "print(\"Elements up to 4th index are:\", arr[:4])\n",
    "print(\"Elements from last backwards are:\", arr[-1::-1])\n",
    "print(\"3 Elements from last backwards are:\", arr[-1:-6:-2])\n",
    "\n",
    "arr2 = np.arange(0,21,2)\n",
    "print(\"New array:\",arr2)\n",
    "print(\"Elements at 2nd, 4th, and 9th index are:\", arr2[[2,4,9]]) # Pass a list as a index to subset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix of random 2-digit numbers\n",
      " [[24 10 16 77 48]\n",
      " [47 60 73 49 26]\n",
      " [63 97 75 21 95]]\n",
      "\n",
      "Double bracket indexing\n",
      "\n",
      "Element in row index 1 and column index 2: 73\n",
      "\n",
      "Single bracket with comma indexing\n",
      "\n",
      "Element in row index 1 and column index 2: 73\n",
      "\n",
      "Row or column extract\n",
      "\n",
      "Entire row at index 2: [63 97 75 21 95]\n",
      "Entire column at index 3: [77 49 21]\n",
      "\n",
      "Subsetting sub-matrices\n",
      "\n",
      "Matrix with row indices 1 and 2 and column indices 3 and 4\n",
      " [[49 26]\n",
      " [21 95]]\n",
      "Matrix with row indices 0 and 1 and column indices 1 and 3\n",
      " [[10 77]\n",
      " [60 49]]\n"
     ]
    }
   ],
   "source": [
    "mat = np.random.randint(10,100,15).reshape(3,5)\n",
    "print(\"Matrix of random 2-digit numbers\\n\",mat)\n",
    "\n",
    "print(\"\\nDouble bracket indexing\\n\")\n",
    "print(\"Element in row index 1 and column index 2:\", mat[1][2])\n",
    "\n",
    "print(\"\\nSingle bracket with comma indexing\\n\")\n",
    "print(\"Element in row index 1 and column index 2:\", mat[1,2])\n",
    "print(\"\\nRow or column extract\\n\")\n",
    "\n",
    "print(\"Entire row at index 2:\", mat[2])\n",
    "print(\"Entire column at index 3:\", mat[:,3])\n",
    "\n",
    "print(\"\\nSubsetting sub-matrices\\n\")\n",
    "print(\"Matrix with row indices 1 and 2 and column indices 3 and 4\\n\", mat[1:3,3:5])\n",
    "print(\"Matrix with row indices 0 and 1 and column indices 1 and 3\\n\", mat[0:2,[1,3]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conditional subsetting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix of random 2-digit numbers\n",
      " [[17 43 10 13 16]\n",
      " [90 46 45 73 94]\n",
      " [83 48 26 21 74]]\n",
      "\n",
      "Elements greater than 50\n",
      " [90 73 94 83 74]\n"
     ]
    }
   ],
   "source": [
    "mat = np.random.randint(10,100,15).reshape(3,5)\n",
    "print(\"Matrix of random 2-digit numbers\\n\",mat)\n",
    "print (\"\\nElements greater than 50\\n\", mat[mat>50])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False],\n",
       "       [ True, False, False,  True,  True],\n",
       "       [ True, False, False, False,  True]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat>50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  0],\n",
       "       [90,  0,  0, 73, 94],\n",
       "       [83,  0,  0,  0, 74]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat*(mat>50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Array operations (array-array, array-scalar, universal functions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "1st Matrix of random single-digit numbers\n",
      " [[6 2 1]\n",
      " [1 1 5]\n",
      " [1 3 5]]\n",
      "\n",
      "2nd Matrix of random single-digit numbers\n",
      " [[8 6 7]\n",
      " [5 9 9]\n",
      " [9 4 7]]\n",
      "\n",
      "Addition\n",
      " [[14  8  8]\n",
      " [ 6 10 14]\n",
      " [10  7 12]]\n",
      "\n",
      "Multiplication\n",
      " [[48 12  7]\n",
      " [ 5  9 45]\n",
      " [ 9 12 35]]\n",
      "\n",
      "Division\n",
      " [[0.75       0.33333333 0.14285714]\n",
      " [0.2        0.11111111 0.55555556]\n",
      " [0.11111111 0.75       0.71428571]]\n",
      "\n",
      "Lineaer combination: 3*A - 2*B\n",
      " [[  2  -6 -11]\n",
      " [ -7 -15  -3]\n",
      " [-15   1   1]]\n",
      "\n",
      "Addition of a scalar (100)\n",
      " [[106 102 101]\n",
      " [101 101 105]\n",
      " [101 103 105]]\n",
      "\n",
      "Exponentiation, matrix cubed here\n",
      " [[216   8   1]\n",
      " [  1   1 125]\n",
      " [  1  27 125]]\n",
      "\n",
      "Exponentiation, sq-root using pow function\n",
      " [[2.44948974 1.41421356 1.        ]\n",
      " [1.         1.         2.23606798]\n",
      " [1.         1.73205081 2.23606798]]\n"
     ]
    }
   ],
   "source": [
    "mat1 = np.random.randint(1,10,9).reshape(3,3)\n",
    "mat2 = np.random.randint(1,10,9).reshape(3,3)\n",
    "print(\"\\n1st Matrix of random single-digit numbers\\n\",mat1)\n",
    "print(\"\\n2nd Matrix of random single-digit numbers\\n\",mat2)\n",
    "\n",
    "print(\"\\nAddition\\n\", mat1+mat2)\n",
    "print(\"\\nMultiplication\\n\", mat1*mat2)\n",
    "print(\"\\nDivision\\n\", mat1/mat2)\n",
    "print(\"\\nLineaer combination: 3*A - 2*B\\n\", 3*mat1-2*mat2)\n",
    "\n",
    "print(\"\\nAddition of a scalar (100)\\n\", 100+mat1)\n",
    "\n",
    "print(\"\\nExponentiation, matrix cubed here\\n\", mat1**3)\n",
    "print(\"\\nExponentiation, sq-root using pow function\\n\",pow(mat1,0.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Labels: ['a', 'b', 'c']\n",
      "My data: [10, 20, 30]\n",
      "Dictionary: {'a': 10, 'b': 20, 'c': 30}\n"
     ]
    }
   ],
   "source": [
    "labels = ['a','b','c']\n",
    "my_data = [10,20,30]\n",
    "arr = np.array(my_data)\n",
    "d = {'a':10,'b':20,'c':30}\n",
    "\n",
    "print (\"Labels:\", labels)\n",
    "print(\"My data:\", my_data)\n",
    "print(\"Dictionary:\", d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    20\n",
      "2    30\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "s1=pd.Series(data=my_data)\n",
    "print(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    10\n",
      "b    20\n",
      "c    30\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "s2=pd.Series(data=my_data, index=labels)\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    10\n",
      "b    20\n",
      "c    30\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "s3=pd.Series(arr, labels)\n",
    "print(s3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    10\n",
      "b    20\n",
      "c    30\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "s4=pd.Series(d)\n",
    "print(s4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The data frame looks like\n",
      "---------------------------------------------\n",
      "    W   X   Y   Z\n",
      "A   3   1   7   1\n",
      "B  14   1   2  12\n",
      "C  14   4  16  11\n",
      "D   4  11  18  15\n",
      "E   1  11  11   3\n"
     ]
    }
   ],
   "source": [
    "matrix_data = np.random.randint(1,20,size=20).reshape(5,4)\n",
    "row_labels = ['A','B','C','D','E']\n",
    "column_headings = ['W','X','Y','Z']\n",
    "\n",
    "df = pd.DataFrame(data=matrix_data, index=row_labels, columns=column_headings)\n",
    "print(\"\\nThe data frame looks like\\n\",'-'*45, sep='')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    a   b   c\n",
      "X  10  30  50\n",
      "Y  20  40  60\n"
     ]
    }
   ],
   "source": [
    "d={'a':[10,20],'b':[30,40],'c':[50,60]}\n",
    "df2=pd.DataFrame(data=d,index=['X','Y'])\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DataFrame can be created reading directly from a CSV or an Excel file\n",
    "\n",
    "Refer to this article, that I wrote for O'Reily Media's Medium publication, to understand various data sources that can be read in Pandas DataFrame directly.\n",
    "\n",
    "**[Read in the data in a Pandas DataFrame like an expert](https://medium.com/97-things/read-in-the-data-in-a-pandas-dataframe-like-an-expert-d03058edae98)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "df3 = pd.read_csv(\"./Data/wine.data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Malic acid</th>\n",
       "      <th>Ash</th>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <th>Magnesium</th>\n",
       "      <th>Total phenols</th>\n",
       "      <th>Flavanoids</th>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <th>Color intensity</th>\n",
       "      <th>Hue</th>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <th>Proline</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>14.23</td>\n",
       "      <td>1.71</td>\n",
       "      <td>2.43</td>\n",
       "      <td>15.6</td>\n",
       "      <td>127</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.06</td>\n",
       "      <td>0.28</td>\n",
       "      <td>2.29</td>\n",
       "      <td>5.64</td>\n",
       "      <td>1.04</td>\n",
       "      <td>3.92</td>\n",
       "      <td>1065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>13.20</td>\n",
       "      <td>1.78</td>\n",
       "      <td>2.14</td>\n",
       "      <td>11.2</td>\n",
       "      <td>100</td>\n",
       "      <td>2.65</td>\n",
       "      <td>2.76</td>\n",
       "      <td>0.26</td>\n",
       "      <td>1.28</td>\n",
       "      <td>4.38</td>\n",
       "      <td>1.05</td>\n",
       "      <td>3.40</td>\n",
       "      <td>1050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>13.16</td>\n",
       "      <td>2.36</td>\n",
       "      <td>2.67</td>\n",
       "      <td>18.6</td>\n",
       "      <td>101</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.24</td>\n",
       "      <td>0.30</td>\n",
       "      <td>2.81</td>\n",
       "      <td>5.68</td>\n",
       "      <td>1.03</td>\n",
       "      <td>3.17</td>\n",
       "      <td>1185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>14.37</td>\n",
       "      <td>1.95</td>\n",
       "      <td>2.50</td>\n",
       "      <td>16.8</td>\n",
       "      <td>113</td>\n",
       "      <td>3.85</td>\n",
       "      <td>3.49</td>\n",
       "      <td>0.24</td>\n",
       "      <td>2.18</td>\n",
       "      <td>7.80</td>\n",
       "      <td>0.86</td>\n",
       "      <td>3.45</td>\n",
       "      <td>1480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>13.24</td>\n",
       "      <td>2.59</td>\n",
       "      <td>2.87</td>\n",
       "      <td>21.0</td>\n",
       "      <td>118</td>\n",
       "      <td>2.80</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.39</td>\n",
       "      <td>1.82</td>\n",
       "      <td>4.32</td>\n",
       "      <td>1.04</td>\n",
       "      <td>2.93</td>\n",
       "      <td>735</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class  Alcohol  Malic acid   Ash  Alcalinity of ash  Magnesium  \\\n",
       "0      1    14.23        1.71  2.43               15.6        127   \n",
       "1      1    13.20        1.78  2.14               11.2        100   \n",
       "2      1    13.16        2.36  2.67               18.6        101   \n",
       "3      1    14.37        1.95  2.50               16.8        113   \n",
       "4      1    13.24        2.59  2.87               21.0        118   \n",
       "\n",
       "   Total phenols  Flavanoids  Nonflavanoid phenols  Proanthocyanins  \\\n",
       "0           2.80        3.06                  0.28             2.29   \n",
       "1           2.65        2.76                  0.26             1.28   \n",
       "2           2.80        3.24                  0.30             2.81   \n",
       "3           3.85        3.49                  0.24             2.18   \n",
       "4           2.80        2.69                  0.39             1.82   \n",
       "\n",
       "   Color intensity   Hue  OD280/OD315 of diluted wines  Proline  \n",
       "0             5.64  1.04                          3.92     1065  \n",
       "1             4.38  1.05                          3.40     1050  \n",
       "2             5.68  1.03                          3.17     1185  \n",
       "3             7.80  0.86                          3.45     1480  \n",
       "4             4.32  1.04                          2.93      735  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "df4 = pd.read_excel(\"./Data/Height_Weight.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ashley</td>\n",
       "      <td>155</td>\n",
       "      <td>140</td>\n",
       "      <td>Palo Alto</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Robin</td>\n",
       "      <td>145</td>\n",
       "      <td>122</td>\n",
       "      <td>Fremont</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Priyanka</td>\n",
       "      <td>152</td>\n",
       "      <td>131</td>\n",
       "      <td>Santa Clara</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Youngchul</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>Cupertino</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zoey</td>\n",
       "      <td>181</td>\n",
       "      <td>190</td>\n",
       "      <td>Hayward</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Height  Weight       Hometown\n",
       "0     Ashley     155     140      Palo Alto\n",
       "1      Robin     145     122        Fremont\n",
       "2   Priyanka     152     131    Santa Clara\n",
       "3  Youngchul     167     148      Cupertino\n",
       "4       Aziz     161     139  San Francisco\n",
       "5       Zoey     181     190        Hayward"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quick checking DataFrames\n",
    "* `.head()`\n",
    "* `.tail()`\n",
    "* `.sample()`\n",
    "* `.info()`\n",
    "* `.describe()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Malic acid</th>\n",
       "      <th>Ash</th>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <th>Magnesium</th>\n",
       "      <th>Total phenols</th>\n",
       "      <th>Flavanoids</th>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <th>Color intensity</th>\n",
       "      <th>Hue</th>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <th>Proline</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>14.23</td>\n",
       "      <td>1.71</td>\n",
       "      <td>2.43</td>\n",
       "      <td>15.6</td>\n",
       "      <td>127</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.06</td>\n",
       "      <td>0.28</td>\n",
       "      <td>2.29</td>\n",
       "      <td>5.64</td>\n",
       "      <td>1.04</td>\n",
       "      <td>3.92</td>\n",
       "      <td>1065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>13.20</td>\n",
       "      <td>1.78</td>\n",
       "      <td>2.14</td>\n",
       "      <td>11.2</td>\n",
       "      <td>100</td>\n",
       "      <td>2.65</td>\n",
       "      <td>2.76</td>\n",
       "      <td>0.26</td>\n",
       "      <td>1.28</td>\n",
       "      <td>4.38</td>\n",
       "      <td>1.05</td>\n",
       "      <td>3.40</td>\n",
       "      <td>1050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>13.16</td>\n",
       "      <td>2.36</td>\n",
       "      <td>2.67</td>\n",
       "      <td>18.6</td>\n",
       "      <td>101</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.24</td>\n",
       "      <td>0.30</td>\n",
       "      <td>2.81</td>\n",
       "      <td>5.68</td>\n",
       "      <td>1.03</td>\n",
       "      <td>3.17</td>\n",
       "      <td>1185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>14.37</td>\n",
       "      <td>1.95</td>\n",
       "      <td>2.50</td>\n",
       "      <td>16.8</td>\n",
       "      <td>113</td>\n",
       "      <td>3.85</td>\n",
       "      <td>3.49</td>\n",
       "      <td>0.24</td>\n",
       "      <td>2.18</td>\n",
       "      <td>7.80</td>\n",
       "      <td>0.86</td>\n",
       "      <td>3.45</td>\n",
       "      <td>1480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>13.24</td>\n",
       "      <td>2.59</td>\n",
       "      <td>2.87</td>\n",
       "      <td>21.0</td>\n",
       "      <td>118</td>\n",
       "      <td>2.80</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.39</td>\n",
       "      <td>1.82</td>\n",
       "      <td>4.32</td>\n",
       "      <td>1.04</td>\n",
       "      <td>2.93</td>\n",
       "      <td>735</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class  Alcohol  Malic acid   Ash  Alcalinity of ash  Magnesium  \\\n",
       "0      1    14.23        1.71  2.43               15.6        127   \n",
       "1      1    13.20        1.78  2.14               11.2        100   \n",
       "2      1    13.16        2.36  2.67               18.6        101   \n",
       "3      1    14.37        1.95  2.50               16.8        113   \n",
       "4      1    13.24        2.59  2.87               21.0        118   \n",
       "\n",
       "   Total phenols  Flavanoids  Nonflavanoid phenols  Proanthocyanins  \\\n",
       "0           2.80        3.06                  0.28             2.29   \n",
       "1           2.65        2.76                  0.26             1.28   \n",
       "2           2.80        3.24                  0.30             2.81   \n",
       "3           3.85        3.49                  0.24             2.18   \n",
       "4           2.80        2.69                  0.39             1.82   \n",
       "\n",
       "   Color intensity   Hue  OD280/OD315 of diluted wines  Proline  \n",
       "0             5.64  1.04                          3.92     1065  \n",
       "1             4.38  1.05                          3.40     1050  \n",
       "2             5.68  1.03                          3.17     1185  \n",
       "3             7.80  0.86                          3.45     1480  \n",
       "4             4.32  1.04                          2.93      735  "
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Malic acid</th>\n",
       "      <th>Ash</th>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <th>Magnesium</th>\n",
       "      <th>Total phenols</th>\n",
       "      <th>Flavanoids</th>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <th>Color intensity</th>\n",
       "      <th>Hue</th>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <th>Proline</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>14.23</td>\n",
       "      <td>1.71</td>\n",
       "      <td>2.43</td>\n",
       "      <td>15.6</td>\n",
       "      <td>127</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.06</td>\n",
       "      <td>0.28</td>\n",
       "      <td>2.29</td>\n",
       "      <td>5.64</td>\n",
       "      <td>1.04</td>\n",
       "      <td>3.92</td>\n",
       "      <td>1065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>13.20</td>\n",
       "      <td>1.78</td>\n",
       "      <td>2.14</td>\n",
       "      <td>11.2</td>\n",
       "      <td>100</td>\n",
       "      <td>2.65</td>\n",
       "      <td>2.76</td>\n",
       "      <td>0.26</td>\n",
       "      <td>1.28</td>\n",
       "      <td>4.38</td>\n",
       "      <td>1.05</td>\n",
       "      <td>3.40</td>\n",
       "      <td>1050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>13.16</td>\n",
       "      <td>2.36</td>\n",
       "      <td>2.67</td>\n",
       "      <td>18.6</td>\n",
       "      <td>101</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.24</td>\n",
       "      <td>0.30</td>\n",
       "      <td>2.81</td>\n",
       "      <td>5.68</td>\n",
       "      <td>1.03</td>\n",
       "      <td>3.17</td>\n",
       "      <td>1185</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class  Alcohol  Malic acid   Ash  Alcalinity of ash  Magnesium  \\\n",
       "0      1    14.23        1.71  2.43               15.6        127   \n",
       "1      1    13.20        1.78  2.14               11.2        100   \n",
       "2      1    13.16        2.36  2.67               18.6        101   \n",
       "\n",
       "   Total phenols  Flavanoids  Nonflavanoid phenols  Proanthocyanins  \\\n",
       "0           2.80        3.06                  0.28             2.29   \n",
       "1           2.65        2.76                  0.26             1.28   \n",
       "2           2.80        3.24                  0.30             2.81   \n",
       "\n",
       "   Color intensity   Hue  OD280/OD315 of diluted wines  Proline  \n",
       "0             5.64  1.04                          3.92     1065  \n",
       "1             4.38  1.05                          3.40     1050  \n",
       "2             5.68  1.03                          3.17     1185  "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Malic acid</th>\n",
       "      <th>Ash</th>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <th>Magnesium</th>\n",
       "      <th>Total phenols</th>\n",
       "      <th>Flavanoids</th>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <th>Color intensity</th>\n",
       "      <th>Hue</th>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <th>Proline</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>171</th>\n",
       "      <td>3</td>\n",
       "      <td>12.77</td>\n",
       "      <td>2.39</td>\n",
       "      <td>2.28</td>\n",
       "      <td>19.5</td>\n",
       "      <td>86</td>\n",
       "      <td>1.39</td>\n",
       "      <td>0.51</td>\n",
       "      <td>0.48</td>\n",
       "      <td>0.64</td>\n",
       "      <td>9.899999</td>\n",
       "      <td>0.57</td>\n",
       "      <td>1.63</td>\n",
       "      <td>470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>172</th>\n",
       "      <td>3</td>\n",
       "      <td>14.16</td>\n",
       "      <td>2.51</td>\n",
       "      <td>2.48</td>\n",
       "      <td>20.0</td>\n",
       "      <td>91</td>\n",
       "      <td>1.68</td>\n",
       "      <td>0.70</td>\n",
       "      <td>0.44</td>\n",
       "      <td>1.24</td>\n",
       "      <td>9.700000</td>\n",
       "      <td>0.62</td>\n",
       "      <td>1.71</td>\n",
       "      <td>660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>173</th>\n",
       "      <td>3</td>\n",
       "      <td>13.71</td>\n",
       "      <td>5.65</td>\n",
       "      <td>2.45</td>\n",
       "      <td>20.5</td>\n",
       "      <td>95</td>\n",
       "      <td>1.68</td>\n",
       "      <td>0.61</td>\n",
       "      <td>0.52</td>\n",
       "      <td>1.06</td>\n",
       "      <td>7.700000</td>\n",
       "      <td>0.64</td>\n",
       "      <td>1.74</td>\n",
       "      <td>740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>174</th>\n",
       "      <td>3</td>\n",
       "      <td>13.40</td>\n",
       "      <td>3.91</td>\n",
       "      <td>2.48</td>\n",
       "      <td>23.0</td>\n",
       "      <td>102</td>\n",
       "      <td>1.80</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.43</td>\n",
       "      <td>1.41</td>\n",
       "      <td>7.300000</td>\n",
       "      <td>0.70</td>\n",
       "      <td>1.56</td>\n",
       "      <td>750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>175</th>\n",
       "      <td>3</td>\n",
       "      <td>13.27</td>\n",
       "      <td>4.28</td>\n",
       "      <td>2.26</td>\n",
       "      <td>20.0</td>\n",
       "      <td>120</td>\n",
       "      <td>1.59</td>\n",
       "      <td>0.69</td>\n",
       "      <td>0.43</td>\n",
       "      <td>1.35</td>\n",
       "      <td>10.200000</td>\n",
       "      <td>0.59</td>\n",
       "      <td>1.56</td>\n",
       "      <td>835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>176</th>\n",
       "      <td>3</td>\n",
       "      <td>13.17</td>\n",
       "      <td>2.59</td>\n",
       "      <td>2.37</td>\n",
       "      <td>20.0</td>\n",
       "      <td>120</td>\n",
       "      <td>1.65</td>\n",
       "      <td>0.68</td>\n",
       "      <td>0.53</td>\n",
       "      <td>1.46</td>\n",
       "      <td>9.300000</td>\n",
       "      <td>0.60</td>\n",
       "      <td>1.62</td>\n",
       "      <td>840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>177</th>\n",
       "      <td>3</td>\n",
       "      <td>14.13</td>\n",
       "      <td>4.10</td>\n",
       "      <td>2.74</td>\n",
       "      <td>24.5</td>\n",
       "      <td>96</td>\n",
       "      <td>2.05</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.56</td>\n",
       "      <td>1.35</td>\n",
       "      <td>9.200000</td>\n",
       "      <td>0.61</td>\n",
       "      <td>1.60</td>\n",
       "      <td>560</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Class  Alcohol  Malic acid   Ash  Alcalinity of ash  Magnesium  \\\n",
       "171      3    12.77        2.39  2.28               19.5         86   \n",
       "172      3    14.16        2.51  2.48               20.0         91   \n",
       "173      3    13.71        5.65  2.45               20.5         95   \n",
       "174      3    13.40        3.91  2.48               23.0        102   \n",
       "175      3    13.27        4.28  2.26               20.0        120   \n",
       "176      3    13.17        2.59  2.37               20.0        120   \n",
       "177      3    14.13        4.10  2.74               24.5         96   \n",
       "\n",
       "     Total phenols  Flavanoids  Nonflavanoid phenols  Proanthocyanins  \\\n",
       "171           1.39        0.51                  0.48             0.64   \n",
       "172           1.68        0.70                  0.44             1.24   \n",
       "173           1.68        0.61                  0.52             1.06   \n",
       "174           1.80        0.75                  0.43             1.41   \n",
       "175           1.59        0.69                  0.43             1.35   \n",
       "176           1.65        0.68                  0.53             1.46   \n",
       "177           2.05        0.76                  0.56             1.35   \n",
       "\n",
       "     Color intensity   Hue  OD280/OD315 of diluted wines  Proline  \n",
       "171         9.899999  0.57                          1.63      470  \n",
       "172         9.700000  0.62                          1.71      660  \n",
       "173         7.700000  0.64                          1.74      740  \n",
       "174         7.300000  0.70                          1.56      750  \n",
       "175        10.200000  0.59                          1.56      835  \n",
       "176         9.300000  0.60                          1.62      840  \n",
       "177         9.200000  0.61                          1.60      560  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.tail(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Malic acid</th>\n",
       "      <th>Ash</th>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <th>Magnesium</th>\n",
       "      <th>Total phenols</th>\n",
       "      <th>Flavanoids</th>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <th>Color intensity</th>\n",
       "      <th>Hue</th>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <th>Proline</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>169</th>\n",
       "      <td>3</td>\n",
       "      <td>13.40</td>\n",
       "      <td>4.60</td>\n",
       "      <td>2.86</td>\n",
       "      <td>25.0</td>\n",
       "      <td>112</td>\n",
       "      <td>1.98</td>\n",
       "      <td>0.96</td>\n",
       "      <td>0.27</td>\n",
       "      <td>1.11</td>\n",
       "      <td>8.50</td>\n",
       "      <td>0.67</td>\n",
       "      <td>1.92</td>\n",
       "      <td>630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>1</td>\n",
       "      <td>13.29</td>\n",
       "      <td>1.97</td>\n",
       "      <td>2.68</td>\n",
       "      <td>16.8</td>\n",
       "      <td>102</td>\n",
       "      <td>3.00</td>\n",
       "      <td>3.23</td>\n",
       "      <td>0.31</td>\n",
       "      <td>1.66</td>\n",
       "      <td>6.00</td>\n",
       "      <td>1.07</td>\n",
       "      <td>2.84</td>\n",
       "      <td>1270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>2</td>\n",
       "      <td>13.11</td>\n",
       "      <td>1.01</td>\n",
       "      <td>1.70</td>\n",
       "      <td>15.0</td>\n",
       "      <td>78</td>\n",
       "      <td>2.98</td>\n",
       "      <td>3.18</td>\n",
       "      <td>0.26</td>\n",
       "      <td>2.28</td>\n",
       "      <td>5.30</td>\n",
       "      <td>1.12</td>\n",
       "      <td>3.18</td>\n",
       "      <td>502</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>117</th>\n",
       "      <td>2</td>\n",
       "      <td>12.42</td>\n",
       "      <td>1.61</td>\n",
       "      <td>2.19</td>\n",
       "      <td>22.5</td>\n",
       "      <td>108</td>\n",
       "      <td>2.00</td>\n",
       "      <td>2.09</td>\n",
       "      <td>0.34</td>\n",
       "      <td>1.61</td>\n",
       "      <td>2.06</td>\n",
       "      <td>1.06</td>\n",
       "      <td>2.96</td>\n",
       "      <td>345</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>130</th>\n",
       "      <td>3</td>\n",
       "      <td>12.86</td>\n",
       "      <td>1.35</td>\n",
       "      <td>2.32</td>\n",
       "      <td>18.0</td>\n",
       "      <td>122</td>\n",
       "      <td>1.51</td>\n",
       "      <td>1.25</td>\n",
       "      <td>0.21</td>\n",
       "      <td>0.94</td>\n",
       "      <td>4.10</td>\n",
       "      <td>0.76</td>\n",
       "      <td>1.29</td>\n",
       "      <td>630</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Class  Alcohol  Malic acid   Ash  Alcalinity of ash  Magnesium  \\\n",
       "169      3    13.40        4.60  2.86               25.0        112   \n",
       "57       1    13.29        1.97  2.68               16.8        102   \n",
       "66       2    13.11        1.01  1.70               15.0         78   \n",
       "117      2    12.42        1.61  2.19               22.5        108   \n",
       "130      3    12.86        1.35  2.32               18.0        122   \n",
       "\n",
       "     Total phenols  Flavanoids  Nonflavanoid phenols  Proanthocyanins  \\\n",
       "169           1.98        0.96                  0.27             1.11   \n",
       "57            3.00        3.23                  0.31             1.66   \n",
       "66            2.98        3.18                  0.26             2.28   \n",
       "117           2.00        2.09                  0.34             1.61   \n",
       "130           1.51        1.25                  0.21             0.94   \n",
       "\n",
       "     Color intensity   Hue  OD280/OD315 of diluted wines  Proline  \n",
       "169             8.50  0.67                          1.92      630  \n",
       "57              6.00  1.07                          2.84     1270  \n",
       "66              5.30  1.12                          3.18      502  \n",
       "117             2.06  1.06                          2.96      345  \n",
       "130             4.10  0.76                          1.29      630  "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.sample(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 178 entries, 0 to 177\n",
      "Data columns (total 14 columns):\n",
      "Class                           178 non-null int64\n",
      "Alcohol                         178 non-null float64\n",
      "Malic acid                      178 non-null float64\n",
      "Ash                             178 non-null float64\n",
      "Alcalinity of ash               178 non-null float64\n",
      "Magnesium                       178 non-null int64\n",
      "Total phenols                   178 non-null float64\n",
      "Flavanoids                      178 non-null float64\n",
      "Nonflavanoid phenols            178 non-null float64\n",
      "Proanthocyanins                 178 non-null float64\n",
      "Color intensity                 178 non-null float64\n",
      "Hue                             178 non-null float64\n",
      "OD280/OD315 of diluted wines    178 non-null float64\n",
      "Proline                         178 non-null int64\n",
      "dtypes: float64(11), int64(3)\n",
      "memory usage: 19.6 KB\n"
     ]
    }
   ],
   "source": [
    "df3.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 6 entries, 0 to 5\n",
      "Data columns (total 4 columns):\n",
      "Name        6 non-null object\n",
      "Height      6 non-null int64\n",
      "Weight      6 non-null int64\n",
      "Hometown    6 non-null object\n",
      "dtypes: int64(2), object(2)\n",
      "memory usage: 320.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "df4.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Class</th>\n",
       "      <td>178.0</td>\n",
       "      <td>1.938202</td>\n",
       "      <td>0.775035</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>2.000</td>\n",
       "      <td>3.0000</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alcohol</th>\n",
       "      <td>178.0</td>\n",
       "      <td>13.000618</td>\n",
       "      <td>0.811827</td>\n",
       "      <td>11.03</td>\n",
       "      <td>12.3625</td>\n",
       "      <td>13.050</td>\n",
       "      <td>13.6775</td>\n",
       "      <td>14.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Malic acid</th>\n",
       "      <td>178.0</td>\n",
       "      <td>2.336348</td>\n",
       "      <td>1.117146</td>\n",
       "      <td>0.74</td>\n",
       "      <td>1.6025</td>\n",
       "      <td>1.865</td>\n",
       "      <td>3.0825</td>\n",
       "      <td>5.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ash</th>\n",
       "      <td>178.0</td>\n",
       "      <td>2.366517</td>\n",
       "      <td>0.274344</td>\n",
       "      <td>1.36</td>\n",
       "      <td>2.2100</td>\n",
       "      <td>2.360</td>\n",
       "      <td>2.5575</td>\n",
       "      <td>3.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <td>178.0</td>\n",
       "      <td>19.494944</td>\n",
       "      <td>3.339564</td>\n",
       "      <td>10.60</td>\n",
       "      <td>17.2000</td>\n",
       "      <td>19.500</td>\n",
       "      <td>21.5000</td>\n",
       "      <td>30.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Magnesium</th>\n",
       "      <td>178.0</td>\n",
       "      <td>99.741573</td>\n",
       "      <td>14.282484</td>\n",
       "      <td>70.00</td>\n",
       "      <td>88.0000</td>\n",
       "      <td>98.000</td>\n",
       "      <td>107.0000</td>\n",
       "      <td>162.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Total phenols</th>\n",
       "      <td>178.0</td>\n",
       "      <td>2.295112</td>\n",
       "      <td>0.625851</td>\n",
       "      <td>0.98</td>\n",
       "      <td>1.7425</td>\n",
       "      <td>2.355</td>\n",
       "      <td>2.8000</td>\n",
       "      <td>3.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Flavanoids</th>\n",
       "      <td>178.0</td>\n",
       "      <td>2.029270</td>\n",
       "      <td>0.998859</td>\n",
       "      <td>0.34</td>\n",
       "      <td>1.2050</td>\n",
       "      <td>2.135</td>\n",
       "      <td>2.8750</td>\n",
       "      <td>5.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <td>178.0</td>\n",
       "      <td>0.361854</td>\n",
       "      <td>0.124453</td>\n",
       "      <td>0.13</td>\n",
       "      <td>0.2700</td>\n",
       "      <td>0.340</td>\n",
       "      <td>0.4375</td>\n",
       "      <td>0.66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <td>178.0</td>\n",
       "      <td>1.590899</td>\n",
       "      <td>0.572359</td>\n",
       "      <td>0.41</td>\n",
       "      <td>1.2500</td>\n",
       "      <td>1.555</td>\n",
       "      <td>1.9500</td>\n",
       "      <td>3.58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Color intensity</th>\n",
       "      <td>178.0</td>\n",
       "      <td>5.058090</td>\n",
       "      <td>2.318286</td>\n",
       "      <td>1.28</td>\n",
       "      <td>3.2200</td>\n",
       "      <td>4.690</td>\n",
       "      <td>6.2000</td>\n",
       "      <td>13.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hue</th>\n",
       "      <td>178.0</td>\n",
       "      <td>0.957449</td>\n",
       "      <td>0.228572</td>\n",
       "      <td>0.48</td>\n",
       "      <td>0.7825</td>\n",
       "      <td>0.965</td>\n",
       "      <td>1.1200</td>\n",
       "      <td>1.71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <td>178.0</td>\n",
       "      <td>2.611685</td>\n",
       "      <td>0.709990</td>\n",
       "      <td>1.27</td>\n",
       "      <td>1.9375</td>\n",
       "      <td>2.780</td>\n",
       "      <td>3.1700</td>\n",
       "      <td>4.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Proline</th>\n",
       "      <td>178.0</td>\n",
       "      <td>746.893258</td>\n",
       "      <td>314.907474</td>\n",
       "      <td>278.00</td>\n",
       "      <td>500.5000</td>\n",
       "      <td>673.500</td>\n",
       "      <td>985.0000</td>\n",
       "      <td>1680.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              count        mean         std     min       25%  \\\n",
       "Class                         178.0    1.938202    0.775035    1.00    1.0000   \n",
       "Alcohol                       178.0   13.000618    0.811827   11.03   12.3625   \n",
       "Malic acid                    178.0    2.336348    1.117146    0.74    1.6025   \n",
       "Ash                           178.0    2.366517    0.274344    1.36    2.2100   \n",
       "Alcalinity of ash             178.0   19.494944    3.339564   10.60   17.2000   \n",
       "Magnesium                     178.0   99.741573   14.282484   70.00   88.0000   \n",
       "Total phenols                 178.0    2.295112    0.625851    0.98    1.7425   \n",
       "Flavanoids                    178.0    2.029270    0.998859    0.34    1.2050   \n",
       "Nonflavanoid phenols          178.0    0.361854    0.124453    0.13    0.2700   \n",
       "Proanthocyanins               178.0    1.590899    0.572359    0.41    1.2500   \n",
       "Color intensity               178.0    5.058090    2.318286    1.28    3.2200   \n",
       "Hue                           178.0    0.957449    0.228572    0.48    0.7825   \n",
       "OD280/OD315 of diluted wines  178.0    2.611685    0.709990    1.27    1.9375   \n",
       "Proline                       178.0  746.893258  314.907474  278.00  500.5000   \n",
       "\n",
       "                                  50%       75%      max  \n",
       "Class                           2.000    3.0000     3.00  \n",
       "Alcohol                        13.050   13.6775    14.83  \n",
       "Malic acid                      1.865    3.0825     5.80  \n",
       "Ash                             2.360    2.5575     3.23  \n",
       "Alcalinity of ash              19.500   21.5000    30.00  \n",
       "Magnesium                      98.000  107.0000   162.00  \n",
       "Total phenols                   2.355    2.8000     3.88  \n",
       "Flavanoids                      2.135    2.8750     5.08  \n",
       "Nonflavanoid phenols            0.340    0.4375     0.66  \n",
       "Proanthocyanins                 1.555    1.9500     3.58  \n",
       "Color intensity                 4.690    6.2000    13.00  \n",
       "Hue                             0.965    1.1200     1.71  \n",
       "OD280/OD315 of diluted wines    2.780    3.1700     4.00  \n",
       "Proline                       673.500  985.0000  1680.00  "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.describe().transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>160.166667</td>\n",
       "      <td>145.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>12.687264</td>\n",
       "      <td>23.748684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>145.000000</td>\n",
       "      <td>122.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>152.750000</td>\n",
       "      <td>133.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>158.000000</td>\n",
       "      <td>139.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>165.500000</td>\n",
       "      <td>146.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>181.000000</td>\n",
       "      <td>190.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Height      Weight\n",
       "count    6.000000    6.000000\n",
       "mean   160.166667  145.000000\n",
       "std     12.687264   23.748684\n",
       "min    145.000000  122.000000\n",
       "25%    152.750000  133.000000\n",
       "50%    158.000000  139.500000\n",
       "75%    165.500000  146.000000\n",
       "max    181.000000  190.000000"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic descriptive statistics on a DataFrame\n",
    "* `mean()`\n",
    "* `std()`\n",
    "* `var()`\n",
    "* `min()` and `max()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Class                             1.938202\n",
       "Alcohol                          13.000618\n",
       "Malic acid                        2.336348\n",
       "Ash                               2.366517\n",
       "Alcalinity of ash                19.494944\n",
       "Magnesium                        99.741573\n",
       "Total phenols                     2.295112\n",
       "Flavanoids                        2.029270\n",
       "Nonflavanoid phenols              0.361854\n",
       "Proanthocyanins                   1.590899\n",
       "Color intensity                   5.058090\n",
       "Hue                               0.957449\n",
       "OD280/OD315 of diluted wines      2.611685\n",
       "Proline                         746.893258\n",
       "dtype: float64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Class                             0.775035\n",
       "Alcohol                           0.811827\n",
       "Malic acid                        1.117146\n",
       "Ash                               0.274344\n",
       "Alcalinity of ash                 3.339564\n",
       "Magnesium                        14.282484\n",
       "Total phenols                     0.625851\n",
       "Flavanoids                        0.998859\n",
       "Nonflavanoid phenols              0.124453\n",
       "Proanthocyanins                   0.572359\n",
       "Color intensity                   2.318286\n",
       "Hue                               0.228572\n",
       "OD280/OD315 of diluted wines      0.709990\n",
       "Proline                         314.907474\n",
       "dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Height    160.966667\n",
       "Weight    564.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.var()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Name           Ashley\n",
       "Height            145\n",
       "Weight            122\n",
       "Hometown    Cupertino\n",
       "dtype: object"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.min()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Indexing, slicing columns and rows of a DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The 'Name' column\n",
      "-------------------------\n",
      "0       Ashley\n",
      "1        Robin\n",
      "2     Priyanka\n",
      "3    Youngchul\n",
      "4         Aziz\n",
      "5         Zoey\n",
      "Name: Name, dtype: object\n",
      "\n",
      "Type of the column: <class 'pandas.core.series.Series'>\n",
      "\n",
      "The 'Name' and 'Weight' columns indexed by passing a list\n",
      "-------------------------------------------------------\n",
      "        Name  Weight\n",
      "0     Ashley     140\n",
      "1      Robin     122\n",
      "2   Priyanka     131\n",
      "3  Youngchul     148\n",
      "4       Aziz     139\n",
      "5       Zoey     190\n",
      "\n",
      "Type of the pair of columns: <class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "print(\"\\nThe 'Name' column\\n\",'-'*25, sep='')\n",
    "print(df4['Name'])\n",
    "print(\"\\nType of the column: \", type(df4['Name']), sep='')\n",
    "print(\"\\nThe 'Name' and 'Weight' columns indexed by passing a list\\n\",'-'*55, sep='')\n",
    "print(df4[['Name','Weight']])\n",
    "print(\"\\nType of the pair of columns: \", type(df4[['Name','Weight']]), sep='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Label-based 'loc' method can be used for selecting row(s)\n",
      "------------------------------------------------------------\n",
      "\n",
      "Single row\n",
      "\n",
      "W    14\n",
      "X     4\n",
      "Y    16\n",
      "Z    11\n",
      "Name: C, dtype: int32\n",
      "\n",
      "Multiple rows\n",
      "\n",
      "    W  X   Y   Z\n",
      "B  14  1   2  12\n",
      "C  14  4  16  11\n",
      "\n",
      "Index position based 'iloc' method can be used for selecting row(s)\n",
      "----------------------------------------------------------------------\n",
      "\n",
      "Single row\n",
      "\n",
      "W    14\n",
      "X     4\n",
      "Y    16\n",
      "Z    11\n",
      "Name: C, dtype: int32\n",
      "\n",
      "Multiple rows\n",
      "\n",
      "    W  X   Y   Z\n",
      "B  14  1   2  12\n",
      "C  14  4  16  11\n"
     ]
    }
   ],
   "source": [
    "print(\"\\nLabel-based 'loc' method can be used for selecting row(s)\\n\",'-'*60, sep='')\n",
    "print(\"\\nSingle row\\n\")\n",
    "print(df.loc['C'])\n",
    "print(\"\\nMultiple rows\\n\")\n",
    "print(df.loc[['B','C']])\n",
    "print(\"\\nIndex position based 'iloc' method can be used for selecting row(s)\\n\",'-'*70, sep='')\n",
    "print(\"\\nSingle row\\n\")\n",
    "print(df.iloc[2])\n",
    "print(\"\\nMultiple rows\\n\")\n",
    "print(df.iloc[[1,2]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conditional subsetting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1    False\n",
       "2    False\n",
       "3     True\n",
       "4     True\n",
       "5     True\n",
       "Name: Height, dtype: bool"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4['Height']>155"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Youngchul</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>Cupertino</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zoey</td>\n",
       "      <td>181</td>\n",
       "      <td>190</td>\n",
       "      <td>Hayward</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Height  Weight       Hometown\n",
       "3  Youngchul     167     148      Cupertino\n",
       "4       Aziz     161     139  San Francisco\n",
       "5       Zoey     181     190        Hayward"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4[df4['Height']>155]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Which students have a **height more than 155 cm and weigh less than 140 lbs**?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Name  Height  Weight       Hometown\n",
       "4  Aziz     161     139  San Francisco"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4[(df4['Height']>155) & (df4['Weight']<140)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Operations on specific columns/rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Malic acid</th>\n",
       "      <th>Ash</th>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <th>Magnesium</th>\n",
       "      <th>Total phenols</th>\n",
       "      <th>Flavanoids</th>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <th>Color intensity</th>\n",
       "      <th>Hue</th>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <th>Proline</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>14.23</td>\n",
       "      <td>1.71</td>\n",
       "      <td>2.43</td>\n",
       "      <td>15.6</td>\n",
       "      <td>127</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.06</td>\n",
       "      <td>0.28</td>\n",
       "      <td>2.29</td>\n",
       "      <td>5.64</td>\n",
       "      <td>1.04</td>\n",
       "      <td>3.92</td>\n",
       "      <td>1065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>13.20</td>\n",
       "      <td>1.78</td>\n",
       "      <td>2.14</td>\n",
       "      <td>11.2</td>\n",
       "      <td>100</td>\n",
       "      <td>2.65</td>\n",
       "      <td>2.76</td>\n",
       "      <td>0.26</td>\n",
       "      <td>1.28</td>\n",
       "      <td>4.38</td>\n",
       "      <td>1.05</td>\n",
       "      <td>3.40</td>\n",
       "      <td>1050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>13.16</td>\n",
       "      <td>2.36</td>\n",
       "      <td>2.67</td>\n",
       "      <td>18.6</td>\n",
       "      <td>101</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.24</td>\n",
       "      <td>0.30</td>\n",
       "      <td>2.81</td>\n",
       "      <td>5.68</td>\n",
       "      <td>1.03</td>\n",
       "      <td>3.17</td>\n",
       "      <td>1185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>14.37</td>\n",
       "      <td>1.95</td>\n",
       "      <td>2.50</td>\n",
       "      <td>16.8</td>\n",
       "      <td>113</td>\n",
       "      <td>3.85</td>\n",
       "      <td>3.49</td>\n",
       "      <td>0.24</td>\n",
       "      <td>2.18</td>\n",
       "      <td>7.80</td>\n",
       "      <td>0.86</td>\n",
       "      <td>3.45</td>\n",
       "      <td>1480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>13.24</td>\n",
       "      <td>2.59</td>\n",
       "      <td>2.87</td>\n",
       "      <td>21.0</td>\n",
       "      <td>118</td>\n",
       "      <td>2.80</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.39</td>\n",
       "      <td>1.82</td>\n",
       "      <td>4.32</td>\n",
       "      <td>1.04</td>\n",
       "      <td>2.93</td>\n",
       "      <td>735</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class  Alcohol  Malic acid   Ash  Alcalinity of ash  Magnesium  \\\n",
       "0      1    14.23        1.71  2.43               15.6        127   \n",
       "1      1    13.20        1.78  2.14               11.2        100   \n",
       "2      1    13.16        2.36  2.67               18.6        101   \n",
       "3      1    14.37        1.95  2.50               16.8        113   \n",
       "4      1    13.24        2.59  2.87               21.0        118   \n",
       "\n",
       "   Total phenols  Flavanoids  Nonflavanoid phenols  Proanthocyanins  \\\n",
       "0           2.80        3.06                  0.28             2.29   \n",
       "1           2.65        2.76                  0.26             1.28   \n",
       "2           2.80        3.24                  0.30             2.81   \n",
       "3           3.85        3.49                  0.24             2.18   \n",
       "4           2.80        2.69                  0.39             1.82   \n",
       "\n",
       "   Color intensity   Hue  OD280/OD315 of diluted wines  Proline  \n",
       "0             5.64  1.04                          3.92     1065  \n",
       "1             4.38  1.05                          3.40     1050  \n",
       "2             5.68  1.03                          3.17     1185  \n",
       "3             7.80  0.86                          3.45     1480  \n",
       "4             4.32  1.04                          2.93      735  "
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### What is the standard deviation of Magnesium and Ash contents for the wine dataset?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Magnesium    14.282484\n",
       "Ash           0.274344\n",
       "dtype: float64"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3[['Magnesium','Ash']].std()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### What is the range of alcohol content in the wine dataset?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The range of alcohol content is:  3.8\n"
     ]
    }
   ],
   "source": [
    "range_alcohol=df3['Alcohol'].max()- df3['Alcohol'].min()\n",
    "print(\"The range of alcohol content is: \", round(range_alcohol,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Top 5 percentile in terms of Flavanoids?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.4975000000000005"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.percentile(df3['Flavanoids'],95)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Malic acid</th>\n",
       "      <th>Ash</th>\n",
       "      <th>Alcalinity of ash</th>\n",
       "      <th>Magnesium</th>\n",
       "      <th>Total phenols</th>\n",
       "      <th>Flavanoids</th>\n",
       "      <th>Nonflavanoid phenols</th>\n",
       "      <th>Proanthocyanins</th>\n",
       "      <th>Color intensity</th>\n",
       "      <th>Hue</th>\n",
       "      <th>OD280/OD315 of diluted wines</th>\n",
       "      <th>Proline</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1</td>\n",
       "      <td>14.75</td>\n",
       "      <td>1.73</td>\n",
       "      <td>2.39</td>\n",
       "      <td>11.4</td>\n",
       "      <td>91</td>\n",
       "      <td>3.10</td>\n",
       "      <td>3.69</td>\n",
       "      <td>0.43</td>\n",
       "      <td>2.81</td>\n",
       "      <td>5.40</td>\n",
       "      <td>1.25</td>\n",
       "      <td>2.73</td>\n",
       "      <td>1150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1</td>\n",
       "      <td>14.38</td>\n",
       "      <td>1.87</td>\n",
       "      <td>2.38</td>\n",
       "      <td>12.0</td>\n",
       "      <td>102</td>\n",
       "      <td>3.30</td>\n",
       "      <td>3.64</td>\n",
       "      <td>0.29</td>\n",
       "      <td>2.96</td>\n",
       "      <td>7.50</td>\n",
       "      <td>1.20</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1</td>\n",
       "      <td>14.19</td>\n",
       "      <td>1.59</td>\n",
       "      <td>2.48</td>\n",
       "      <td>16.5</td>\n",
       "      <td>108</td>\n",
       "      <td>3.30</td>\n",
       "      <td>3.93</td>\n",
       "      <td>0.32</td>\n",
       "      <td>1.86</td>\n",
       "      <td>8.70</td>\n",
       "      <td>1.23</td>\n",
       "      <td>2.82</td>\n",
       "      <td>1680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>1</td>\n",
       "      <td>13.88</td>\n",
       "      <td>1.89</td>\n",
       "      <td>2.59</td>\n",
       "      <td>15.0</td>\n",
       "      <td>101</td>\n",
       "      <td>3.25</td>\n",
       "      <td>3.56</td>\n",
       "      <td>0.17</td>\n",
       "      <td>1.70</td>\n",
       "      <td>5.43</td>\n",
       "      <td>0.88</td>\n",
       "      <td>3.56</td>\n",
       "      <td>1095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>1</td>\n",
       "      <td>13.94</td>\n",
       "      <td>1.73</td>\n",
       "      <td>2.27</td>\n",
       "      <td>17.4</td>\n",
       "      <td>108</td>\n",
       "      <td>2.88</td>\n",
       "      <td>3.54</td>\n",
       "      <td>0.32</td>\n",
       "      <td>2.08</td>\n",
       "      <td>8.90</td>\n",
       "      <td>1.12</td>\n",
       "      <td>3.10</td>\n",
       "      <td>1260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>1</td>\n",
       "      <td>13.82</td>\n",
       "      <td>1.75</td>\n",
       "      <td>2.42</td>\n",
       "      <td>14.0</td>\n",
       "      <td>111</td>\n",
       "      <td>3.88</td>\n",
       "      <td>3.74</td>\n",
       "      <td>0.32</td>\n",
       "      <td>1.87</td>\n",
       "      <td>7.05</td>\n",
       "      <td>1.01</td>\n",
       "      <td>3.26</td>\n",
       "      <td>1190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>1</td>\n",
       "      <td>13.72</td>\n",
       "      <td>1.43</td>\n",
       "      <td>2.50</td>\n",
       "      <td>16.7</td>\n",
       "      <td>108</td>\n",
       "      <td>3.40</td>\n",
       "      <td>3.67</td>\n",
       "      <td>0.19</td>\n",
       "      <td>2.04</td>\n",
       "      <td>6.80</td>\n",
       "      <td>0.89</td>\n",
       "      <td>2.87</td>\n",
       "      <td>1285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>2</td>\n",
       "      <td>12.37</td>\n",
       "      <td>1.07</td>\n",
       "      <td>2.10</td>\n",
       "      <td>18.5</td>\n",
       "      <td>88</td>\n",
       "      <td>3.52</td>\n",
       "      <td>3.75</td>\n",
       "      <td>0.24</td>\n",
       "      <td>1.95</td>\n",
       "      <td>4.50</td>\n",
       "      <td>1.04</td>\n",
       "      <td>2.77</td>\n",
       "      <td>660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>121</th>\n",
       "      <td>2</td>\n",
       "      <td>11.56</td>\n",
       "      <td>2.05</td>\n",
       "      <td>3.23</td>\n",
       "      <td>28.5</td>\n",
       "      <td>119</td>\n",
       "      <td>3.18</td>\n",
       "      <td>5.08</td>\n",
       "      <td>0.47</td>\n",
       "      <td>1.87</td>\n",
       "      <td>6.00</td>\n",
       "      <td>0.93</td>\n",
       "      <td>3.69</td>\n",
       "      <td>465</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Class  Alcohol  Malic acid   Ash  Alcalinity of ash  Magnesium  \\\n",
       "13       1    14.75        1.73  2.39               11.4         91   \n",
       "14       1    14.38        1.87  2.38               12.0        102   \n",
       "18       1    14.19        1.59  2.48               16.5        108   \n",
       "42       1    13.88        1.89  2.59               15.0        101   \n",
       "49       1    13.94        1.73  2.27               17.4        108   \n",
       "52       1    13.82        1.75  2.42               14.0        111   \n",
       "58       1    13.72        1.43  2.50               16.7        108   \n",
       "98       2    12.37        1.07  2.10               18.5         88   \n",
       "121      2    11.56        2.05  3.23               28.5        119   \n",
       "\n",
       "     Total phenols  Flavanoids  Nonflavanoid phenols  Proanthocyanins  \\\n",
       "13            3.10        3.69                  0.43             2.81   \n",
       "14            3.30        3.64                  0.29             2.96   \n",
       "18            3.30        3.93                  0.32             1.86   \n",
       "42            3.25        3.56                  0.17             1.70   \n",
       "49            2.88        3.54                  0.32             2.08   \n",
       "52            3.88        3.74                  0.32             1.87   \n",
       "58            3.40        3.67                  0.19             2.04   \n",
       "98            3.52        3.75                  0.24             1.95   \n",
       "121           3.18        5.08                  0.47             1.87   \n",
       "\n",
       "     Color intensity   Hue  OD280/OD315 of diluted wines  Proline  \n",
       "13              5.40  1.25                          2.73     1150  \n",
       "14              7.50  1.20                          3.00     1547  \n",
       "18              8.70  1.23                          2.82     1680  \n",
       "42              5.43  0.88                          3.56     1095  \n",
       "49              8.90  1.12                          3.10     1260  \n",
       "52              7.05  1.01                          3.26     1190  \n",
       "58              6.80  0.89                          2.87     1285  \n",
       "98              4.50  1.04                          2.77      660  \n",
       "121             6.00  0.93                          3.69      465  "
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3[df3['Flavanoids']>=3.4975]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Show the average alcohol, ash, and magnesium content of the wine brands which rank top 5 percent in terms of flavanoids**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Ash            2.484444\n",
       "Alcohol       13.623333\n",
       "Magnesium    104.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3[df3['Flavanoids']>=3.4975][['Ash','Alcohol','Magnesium']].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a new column as a function of mathematical operations on existing columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ashley</td>\n",
       "      <td>155</td>\n",
       "      <td>140</td>\n",
       "      <td>Palo Alto</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Robin</td>\n",
       "      <td>145</td>\n",
       "      <td>122</td>\n",
       "      <td>Fremont</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Priyanka</td>\n",
       "      <td>152</td>\n",
       "      <td>131</td>\n",
       "      <td>Santa Clara</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Youngchul</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>Cupertino</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zoey</td>\n",
       "      <td>181</td>\n",
       "      <td>190</td>\n",
       "      <td>Hayward</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Height  Weight       Hometown\n",
       "0     Ashley     155     140      Palo Alto\n",
       "1      Robin     145     122        Fremont\n",
       "2   Priyanka     152     131    Santa Clara\n",
       "3  Youngchul     167     148      Cupertino\n",
       "4       Aziz     161     139  San Francisco\n",
       "5       Zoey     181     190        Hayward"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "      <th>BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ashley</td>\n",
       "      <td>155</td>\n",
       "      <td>140</td>\n",
       "      <td>Palo Alto</td>\n",
       "      <td>26.432000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Robin</td>\n",
       "      <td>145</td>\n",
       "      <td>122</td>\n",
       "      <td>Fremont</td>\n",
       "      <td>26.320202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Priyanka</td>\n",
       "      <td>152</td>\n",
       "      <td>131</td>\n",
       "      <td>Santa Clara</td>\n",
       "      <td>25.718729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Youngchul</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>Cupertino</td>\n",
       "      <td>24.071001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>24.323633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zoey</td>\n",
       "      <td>181</td>\n",
       "      <td>190</td>\n",
       "      <td>Hayward</td>\n",
       "      <td>26.306425</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Height  Weight       Hometown        BMI\n",
       "0     Ashley     155     140      Palo Alto  26.432000\n",
       "1      Robin     145     122        Fremont  26.320202\n",
       "2   Priyanka     152     131    Santa Clara  25.718729\n",
       "3  Youngchul     167     148      Cupertino  24.071001\n",
       "4       Aziz     161     139  San Francisco  24.323633\n",
       "5       Zoey     181     190        Hayward  26.306425"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4['BMI']=df4['Weight']*0.453592/(df4['Height']/100)**2\n",
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "      <th>BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Youngchul</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>Cupertino</td>\n",
       "      <td>24.071001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>24.323633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Priyanka</td>\n",
       "      <td>152</td>\n",
       "      <td>131</td>\n",
       "      <td>Santa Clara</td>\n",
       "      <td>25.718729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zoey</td>\n",
       "      <td>181</td>\n",
       "      <td>190</td>\n",
       "      <td>Hayward</td>\n",
       "      <td>26.306425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Robin</td>\n",
       "      <td>145</td>\n",
       "      <td>122</td>\n",
       "      <td>Fremont</td>\n",
       "      <td>26.320202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ashley</td>\n",
       "      <td>155</td>\n",
       "      <td>140</td>\n",
       "      <td>Palo Alto</td>\n",
       "      <td>26.432000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Height  Weight       Hometown        BMI\n",
       "3  Youngchul     167     148      Cupertino  24.071001\n",
       "4       Aziz     161     139  San Francisco  24.323633\n",
       "2   Priyanka     152     131    Santa Clara  25.718729\n",
       "5       Zoey     181     190        Hayward  26.306425\n",
       "1      Robin     145     122        Fremont  26.320202\n",
       "0     Ashley     155     140      Palo Alto  26.432000"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.sort_values(by='BMI')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use `inplace=True` to make the changes reflected on the original DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "      <th>BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ashley</td>\n",
       "      <td>155</td>\n",
       "      <td>140</td>\n",
       "      <td>Palo Alto</td>\n",
       "      <td>26.432000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Robin</td>\n",
       "      <td>145</td>\n",
       "      <td>122</td>\n",
       "      <td>Fremont</td>\n",
       "      <td>26.320202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Priyanka</td>\n",
       "      <td>152</td>\n",
       "      <td>131</td>\n",
       "      <td>Santa Clara</td>\n",
       "      <td>25.718729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Youngchul</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>Cupertino</td>\n",
       "      <td>24.071001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>24.323633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zoey</td>\n",
       "      <td>181</td>\n",
       "      <td>190</td>\n",
       "      <td>Hayward</td>\n",
       "      <td>26.306425</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Height  Weight       Hometown        BMI\n",
       "0     Ashley     155     140      Palo Alto  26.432000\n",
       "1      Robin     145     122        Fremont  26.320202\n",
       "2   Priyanka     152     131    Santa Clara  25.718729\n",
       "3  Youngchul     167     148      Cupertino  24.071001\n",
       "4       Aziz     161     139  San Francisco  24.323633\n",
       "5       Zoey     181     190        Hayward  26.306425"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "df4.sort_values(by='BMI',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Hometown</th>\n",
       "      <th>BMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Youngchul</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>Cupertino</td>\n",
       "      <td>24.071001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Aziz</td>\n",
       "      <td>161</td>\n",
       "      <td>139</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>24.323633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Priyanka</td>\n",
       "      <td>152</td>\n",
       "      <td>131</td>\n",
       "      <td>Santa Clara</td>\n",
       "      <td>25.718729</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Zoey</td>\n",
       "      <td>181</td>\n",
       "      <td>190</td>\n",
       "      <td>Hayward</td>\n",
       "      <td>26.306425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Robin</td>\n",
       "      <td>145</td>\n",
       "      <td>122</td>\n",
       "      <td>Fremont</td>\n",
       "      <td>26.320202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ashley</td>\n",
       "      <td>155</td>\n",
       "      <td>140</td>\n",
       "      <td>Palo Alto</td>\n",
       "      <td>26.432000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Height  Weight       Hometown        BMI\n",
       "3  Youngchul     167     148      Cupertino  24.071001\n",
       "4       Aziz     161     139  San Francisco  24.323633\n",
       "2   Priyanka     152     131    Santa Clara  25.718729\n",
       "5       Zoey     181     190        Hayward  26.306425\n",
       "1      Robin     145     122        Fremont  26.320202\n",
       "0     Ashley     155     140      Palo Alto  26.432000"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
